#include "SerializeManager.h"


Shape SerializeManager::getARect(int x, int y, int h, int w)
{
    return shapeFactory.createRect(x, y, h, w);
}

Shape SerializeManager::getACircle(int x, int y, int h, int w)
{
    return shapeFactory.createCircle(x, y, h, w);
}

Shape SerializeManager::getABackout()
{
    return shapeFactory.createBackout();
}

Shape SerializeManager::getAClear()
{
    return shapeFactory.createClear();
}

bool SerializeManager::saveToFile(ShapeSet *sets, QString& path)
{
    fstream output(path.toStdString(), ios::out | ios::trunc | ios::binary);
    if (!output)
    {
        return false;
    }
    if (!sets->SerializeToOstream(&output))
    {
        output.close();
        return false;
    }
    output.close();
    return true;
}

bool SerializeManager::parseFromFile(ShapeSet *sets, QString& path)
{
    fstream input(path.toStdString(), ios::in | ios::binary);
    if (!input)
    {
        return false;
    }
    if (!sets->ParseFromIstream(&input))
    {
        input.close();
        return false;
    }
    input.close();
    return true;
}

bool SerializeManager::saveToFile(QVector<Shape> &sets, QString &path)
{
    ShapeSet* sps = new ShapeSet();
    shapePacker.packToShapeSet(sps, sets);
    bool ret = saveToFile(sps, path);
    delete sps;
    return ret;
}

bool SerializeManager::parseFromFile(QVector<Shape> &sets, QString &path)
{
    ShapeSet* sps = new ShapeSet();
    bool ret = parseFromFile(sps, path);
    shapePacker.depackFromShapeSet(sps, sets);
    delete sps;
    return ret;
}

void SerializeManager::packToShapeSet(ShapeSet *sets, QVector<Shape> &vec)
{
    shapePacker.packToShapeSet(sets, vec);
}

void SerializeManager::depackFromShapeSet(ShapeSet *sets, QVector<Shape> &vec)
{
    shapePacker.depackFromShapeSet(sets, vec);
}

SerializeManager *SerializeManager::instance()
{
    return Sington<SerializeManager>::getInstance();
}
